<?php

/**
 * uniBoard Language edit
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_lang{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Language API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Language Copy API
	 *
	 * @var object
	 */
	
	private $copy_api		= NULL;
	
	/**
	 * Languages Cache
	 *
	 * @var array
	 */
	
	private $languages		= array();
	
	/**
	 * Language strings from loaded file
	 *
	 * @var array
	 */
	
	private $lang			= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Start Language API
		$this -> api = new api_language( $appCore, $appCore -> _REQ( 'lang'));
		
		// Did we loaded api from DB?
		if ( !$this -> api -> data_loaded)
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['acp_language_not_found'], $appCore -> lang['acp_languages_list']);
			
			// Trow user back to languages list
			new action_langs( $appCore);	
		}
		else if ( $appCore -> _REQ( 'do') == 'details')
		{
			// Update language?
			if ( $appCore -> _POST( 'update') == 1)
			{
				// Fill API
				$this -> api -> setLanguageName( $appCore -> _POST( 'lang_name'));
				$this -> api -> setLanguageIso( $appCore -> _POST( 'lang_iso'));
				$this -> api -> setLanguageAuthor( $appCore -> _POST( 'lang_author'));
				$this -> api -> setLanguageEmail( $appCore -> _POST( 'lang_email'));
				$this -> api -> setLanguageWww( $appCore -> _POST( 'lang_www'));
									
				// Error?
				if ( $this -> api -> error)
				{
					// Draw error messages
					$appCore -> output -> error( join( '<br />', $this -> api -> errors), htmlspecialchars( $this -> api -> language_name));
								
					// Repeat form
					$this -> drawDetailsForm();
				}
				else
				{
					// Save changes
					$this -> api -> saveData();
					
					// Draw message
					$appCore -> output -> message( $appCore -> lang['acp_language_details_updated'], htmlspecialchars( $this -> api -> language_name));
					
					// Set as default?
					if ( $this -> api -> language_id != $appCore -> conf['default_language'] && $appCore -> _POST( 'lang_make_default') == 1)
					{
						$appCore -> setConfig( 'default_language', $this -> api -> language_id);	
					}
					
					// Create Log
					$appCore -> logAdmin( 'log_admin_changed_language_details', htmlspecialchars( $this -> api -> language_name));
					
					// Display languages list
					new action_langs( $appCore);
				}
			}
			else
			{	
				// Draw details form
				$this -> drawDetailsForm();
			}
		}
		else if ( $appCore -> _REQ( 'do') == 'users')
		{
			// Build Languages cache
			$this -> languages = $appCore -> getLanguages();
			
			// Change users?
			if ( $appCore -> _POST( 'update') == 1)
			{
				// Grab new language
				$new_lang = (integer) $appCore -> _POST( 'lang_replace');
				
				// Style not in array?
				if ( !isset( $this -> languages[$new_lang]))
				{
					// Show error
					$appCore -> output -> error( $appCore -> lang['acp_language_not_found'], htmlspecialchars( $this -> api -> language_name));
					
					// Draw form
					$this -> drawUsersForm();
				}
				else if ( $new_lang == $this -> api -> language_id)
				{
					// Show error
					$appCore -> output -> error( $appCore -> lang['acp_language_new_language_same'], htmlspecialchars( $this -> api -> language_name));
					
					// Draw form
					$this -> drawUsersForm();
				}
				else
				{
					// Reassign users
					$appCore -> db -> update( array( 'user_language' => $new_lang), 'users', 'user_language = \'' . $this -> api -> language_id . '\' AND user_id > \'0\'');
					
					// Create Log
					$appCore -> logAdmin( 'log_admin_changed_language_users', htmlspecialchars( $this -> api -> language_name));
					
					// Redirect user to langs list
					$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=langs'), $appCore -> lang['acp_style_users_updated']);		
				}
			}
			else
			{
				// Draw form
				$this -> drawUsersForm();
			}
		}
		else if ( $appCore -> _REQ( 'do') == 'copy')
		{
			// Create api copy
			$this -> copy_api = clone $this -> api;
			
			// Fix id
			$this -> copy_api -> language_id = 0;
			
			// Submit form?
			if ( $appCore -> _POST( 'update') == 1)
			{
				// Fill api name things
				$this -> copy_api -> setLanguageName( $appCore -> _POST( 'lang_name'));
				$this -> copy_api -> setLanguageIso( $appCore -> _POST( 'lang_iso'));
				$this -> copy_api -> setLanguageAuthor( $this -> api -> language_author);
				$this -> copy_api -> setLanguageEmail( $this -> api -> language_email);
				$this -> copy_api -> setLanguageWww( $this -> api -> language_www);
				
				// Api errors?
				if ( $this -> copy_api -> error)
				{
					// Draw error
					$appCore -> output -> error( $this -> copy_api -> last_error, $appCore -> lang['language_op_copy']);
					
					// Repeat form
					$this -> drawCopyForm();
				}
				else
				{
					// Save api
					$this -> copy_api -> saveData();
						
					// Create new directory
					class_files::newDir( CACHE_PATH . 'lang_' . $this -> copy_api -> language_id);
					
					// Get files list
					$files_list = $appCore -> getLanguageFiles();
					
					// Copy files
					foreach ( $files_list as $file)
					{
						// Source exists?
						if ( file_exists( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $file . PHP_EXT))
						{
							// Make new file
							file_put_contents( CACHE_PATH . 'lang_' . $this -> copy_api -> language_id . '/' . $file . PHP_EXT, file_get_contents( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $file . PHP_EXT));
						}
					}
						
					// Create Log
					$appCore -> logAdmin( 'log_admin_copied_language', htmlspecialchars( $this -> api -> language_name));
								
					// Redirect us to new page
					$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=langs'), $appCore -> lang['acp_language_copy_created']);
				}
			}
			else
			{
				// Display copy form
				$this -> drawCopyForm();	
			}
		}
		else if ( $appCore -> _REQ( 'do') == 'export')
		{	
			// Create Log
			$appCore -> logAdmin( 'log_admin_exported_language', htmlspecialchars( $this -> api -> language_name));
					
			// Perform export
			$this -> makeExport();
		}
		else if ( $appCore -> _REQ( 'do') == 'delete')
		{
			// Default language?
			if ( $this -> api -> language_id == $appCore -> conf['default_language'])
			{
				// Set error
				$appCore -> output -> error( $appCore -> lang['acp_language_default_delete'], $appCore -> lang['language_op_delete']);
				
				// Hop back to langs list
				new action_langs( $appCore);
			}
			else
			{
				// Build Languages cache
				$this -> languages = $appCore -> getLanguages();
			
				// Update?
				if ( $appCore -> _POST( 'delete') == 1)
				{
					// Get user's choice
					$choice = (int) $appCore -> _POST( 'lang_replace');
					
					// Choice incorrect?
					if ( !isset( $this -> languages[$choice]))
					{
						// Set error
						$appCore -> output -> error( $appCore -> lang['acp_language_not_found'], $appCore -> lang['language_op_delete']);
						
						// Draw delete form
						$this -> drawDeleteForm();
					}
					else if ( $choice == $this -> api -> language_id)
					{
						// Set error
						$appCore -> output -> error( $appCore -> lang['acp_language_new_language_same'], $appCore -> lang['language_op_delete']);
						
						// Draw delete form
						$this -> drawDeleteForm();
					}
					else
					{
						// Delete language
						$this -> deleteLanguage( $choice);
						
						// Create Log
						$appCore -> logAdmin( 'log_admin_deleted_language', htmlspecialchars( $this -> api -> language_name));
			
						// Redirect us
						$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=langs'), $appCore -> lang['acp_language_deleted']);
					}
				}
				else
				{
					// Draw delete form
					$this -> drawDeleteForm();
				}
			}
		}
		else
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['wrong_action_code'], $appCore -> lang['acp_languages_list']);
			
			// Trow user back to languages list
			new action_langs( $appCore);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws details form
	 *
	 */
	
	function drawDetailsForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_lang&do=details&lang=' . $this -> api -> language_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['language_form_sect_basic']);

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['language_form_name'], $this -> appCore -> drawTextInput( 'lang_name', $this -> api -> language_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['language_form_iso'], $this -> appCore -> drawTextInput( 'lang_iso', $this -> api -> language_iso), $this -> appCore -> lang['language_form_iso_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['language_form_set_as_default'], ( $this -> api -> language_id == $this -> appCore -> conf['default_language'] ? $this -> appCore -> lang['language_form_language_default'] : $this -> appCore -> drawYesNo( 'default_language', $this -> appCore -> _POST( 'lang_make_default'))));
			
		// Author block
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['language_form_sect_author']);

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['language_form_author_name'], $this -> appCore -> drawTextInput( 'lang_author', $this -> api -> language_author));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['language_form_author_email'], $this -> appCore -> drawTextInput( 'lang_email', $this -> api -> language_email));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['language_form_author_www'], $this -> appCore -> drawTextInput( 'lang_www', $this -> api -> language_www));
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['language_op_edit'], array(
			$this -> appCore -> adminLink( 'act=langs') => $this -> appCore -> lang['acp_languages_list'],
			'' => htmlspecialchars( $this -> api -> language_name)
		), ob_get_clean());
	
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Draws move-users form
	 *
	 */
	
	function drawUsersForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_lang&do=users&lang=' . $this -> api -> language_id), 'editor_form');
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock();

		// Draw style select
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['language_form_change_style'], $this -> appCore -> drawList( 'lang_replace', $this -> languages, $this -> api -> language_id));
			
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['run_tool']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['language_op_users'], array(
			$this -> appCore -> adminLink( 'act=langs') => $this -> appCore -> lang['acp_languages_list'],
			'' => htmlspecialchars( $this -> api -> language_name)
		), ob_get_clean());
	
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Draws move-users form
	 *
	 */
	
	function drawCopyForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_lang&do=copy&lang=' . $this -> api -> language_id));
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['language_form_sect_basic']);

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['new_language_form_name'], $this -> appCore -> drawTextInput( 'lang_name', $this -> copy_api -> language_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['new_language_form_iso'], $this -> appCore -> drawTextInput( 'lang_iso', $this -> copy_api -> language_iso));
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['language_op_copy'], array(
			$this -> appCore -> adminLink( 'act=langs') => $this -> appCore -> lang['acp_languages_list'],
			'' => htmlspecialchars( $this -> api -> language_name)
		), ob_get_clean());
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Performs export
	 *
	 */
	
	function makeExport(){
		
		// Start content
		$file_content = '<?xml version="1.0" encoding="UTF-8"?>
<uniboard_lang_export>
	<language_data>
		<language_name>' . base64_encode( $this -> api -> language_name) .'</language_name>
		<language_iso>' . base64_encode( $this -> api -> language_iso) .'</language_iso>
		<language_author>' . base64_encode( $this -> api -> language_author) .'</language_author>
		<language_email>' . base64_encode( $this -> api -> language_email) .'</language_email>
		<language_www>' . base64_encode( $this -> api -> language_www) .'</language_www>
	</language_data>
	<language_files>';
			
		// Export base files only
		$lang_files = $this -> appCore -> getLanguageFiles( false);
		
		// Reiterate files
		foreach ( $lang_files as $file_name)
		{
			// File exists?
			if ( file_exists( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $file_name))
			{
				// File TXT?
				$file_txt = substr( $file_name, strrpos( $file_name, '.')) == '.txt';
				
				// Handle those files differently
				if ( $file_txt)
				{
					// Open file
					$file_content .= '
		<file>
			<file_name>' . base64_encode( substr( $file_name, 0, strrpos( $file_name, '.'))) . '</file_name>
			<file_type>' . base64_encode( 'txt') . '</file_type>
			<string>' . base64_encode( file_get_contents( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $file_name)) . '</string>
		</file>';
				}
				else
				{
					// Include lang file
					include( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $file_name);
					
					// Lang has content?
					if ( !empty( $this -> lang))
					{
						// Open file
						$file_content .= '
		<file>
			<file_name>' . base64_encode( substr( $file_name, 0, strrpos( $file_name, '.'))) . '</file_name>
			<file_type>' . base64_encode( 'php') . '</file_type>
			<strings>';
						
						// Reiterate lang
						foreach ( $this -> lang as $key => $string)
						{
							$file_content .= '
				<string>
					<string_id>' . base64_encode( $key) . '</string_id>
					<string_content>' . base64_encode( $string) . '</string_content>
				</string>';
						}
						
						// Close file
						$file_content .= '
			</strings>
		</file>';
					}
					
					// Clear cache
					$this -> lang = array();
				}
			}
		}
		
		// Close content
		$file_content .= '
	</language_files>
</uniboard_lang_export>';
		
		// Compress it with GZIP?
		if ( function_exists( 'gzencode'))
		{
			// Compress file with gzip
			$file_content = gzencode( $file_content, 9);
			
			// Send non-compressed XML file
			$this -> appCore -> fileHeaders( 'application/x-compressed', strlen( $file_content), $this -> appCore -> secureFilename( $this -> api -> language_name) . '.xml.gz');
			
			// Echo file content
			exit( $file_content);
		}
		else
		{
			// Send non-compressed XML file
			$this -> appCore -> fileHeaders( 'application/xml', strlen( $file_content), $this -> appCore -> secureFilename( $this -> api -> language_name) . '.xml');
			
			// Echo file content
			exit( $file_content);
		}
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Draws delete form
	 *
	 */
	
	function drawDeleteForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_lang&do=delete&lang=' . $this -> api -> language_id));
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'delete', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock();

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['language_form_replace_lang'], $this -> appCore -> drawList( 'lang_replace', $this -> languages, $this -> appCore -> _POST( 'lang_replace')), $this -> appCore -> lang['language_form_replace_lang_expl']);
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['delete']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['language_op_delete'], array(
			$this -> appCore -> adminLink( 'act=langs') => $this -> appCore -> lang['acp_languages_list'],
			'' => htmlspecialchars( $this -> api -> language_name)
		), ob_get_clean());
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Deletes language and replaces it with new one
	 *
	 * @param integer $replace
	 */
	
	function deleteLanguage( $replace){
		
		// Update users selection
		$this -> appCore -> db -> update( array( 'user_language' => $replace), 'users', 'user_language = \'' . $this -> api -> language_id . '\'');
		
		// Delete lang from DB
		$this -> appCore -> db -> delete( 'languages', 'language_id = \'' . $this -> api -> language_id . '\'');
		
		// Lang files
		$lang_files = $this -> appCore -> getLanguageFiles();
		
		// Delete files
		foreach ( $lang_files as $file)
		{
			// Delete file
			class_files::unlink( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/' . $file . PHP_EXT);
		}
		
		// Delete Dir INDEX
		class_files::unlink( CACHE_PATH . 'lang_' . $this -> api -> language_id . '/index.html');
		
		// Delete directory
		class_files::deleteDir( CACHE_PATH . 'lang_' . $this -> api -> language_id);
		
		// Flush cache
		class_cache::flushCache( 'languages');
		
	}
	
}